package faircat.android.exphone;

import java.util.List;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class NewSMSActivity extends Activity implements OnClickListener {
	ContentResolver mCr;
	int mSimCard = 0;
	private EditText mReceiver;
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sms_new);
		mReceiver = (EditText)findViewById(R.id.receiver);
		mSimCard = getIntent().getIntExtra("sim", 0);
		String number = getIntent().getStringExtra("number");
		if(number != null){
			mReceiver.setText(number);
			mReceiver.setSelection(number.length());
		}
		findViewById(R.id.send_sms).setOnClickListener(this);
	}

	
	void sendMessage1(String number, String body){
		if(body.length()==0)
		{
		    Toast.makeText(NewSMSActivity.this, getResources().getString(R.string.smsContent_empty_assist), Toast.LENGTH_LONG).show();
		    return;
		}
		if(number.length()==0)
		{
		    Toast.makeText(NewSMSActivity.this, getResources().getString(R.string.revicer_empty_assist), Toast.LENGTH_LONG).show();
		    return;
		}		
	    SmsManager smsManager = SmsManager.getDefault();
	    PendingIntent sentIntent = PendingIntent.getBroadcast(NewSMSActivity.this, 0, new Intent(), 0);
	    if(body.length()>70)
	    {
	    	List<String> msgs = smsManager.divideMessage(body);
	        for (String msg : msgs) 
	        {
	            smsManager.sendTextMessage(number, null, msg, sentIntent, null);                        
	        }
	    }
	    else
	    {
	    	smsManager.sendTextMessage(number, null, body, sentIntent, null);

	    }
	    Toast.makeText(NewSMSActivity.this, getResources().getString(R.string.sms_send_success), Toast.LENGTH_LONG).show();

	    {
	    	//content://sms/sent    
	    		ContentValues cv = new ContentValues();    
	    		//cv.put("_id", "99");    
	    		//cv.put("thread_id", "0");    
	    		cv.put("address", number);    
	    		cv.put("person", "");    
	    		//cv.put("date", "9999"); 
	    		//cv.put("protocol", "0"); 
	    		//cv.put("read", "1"); 
	    		//cv.put("status", "-1"); 
	    		cv.put("type", "2"); 
	    		cv.put("body", body); 
	    		this.getContentResolver().insert(Uri.parse("content://sms/sent"), cv); 
	    }
	   
	}
	
	void sendMessage2(String number, String body){
		 if(ATSession.mState==3)
		 {
		    Toast.makeText(NewSMSActivity.this, getResources().getString(R.string.connection_has_broken), Toast.LENGTH_LONG).show();
		    return;			 
		 }		
		if(body.length()==0)
		{
		    Toast.makeText(NewSMSActivity.this, getResources().getString(R.string.smsContent_empty_assist), Toast.LENGTH_LONG).show();
		    return;
		}
		if(number.length()==0)
		{
		    Toast.makeText(NewSMSActivity.this, getResources().getString(R.string.revicer_empty_assist), Toast.LENGTH_LONG).show();
		    return;
		}	
		Log.i("aaaaaaaaaa", "smsCenterNm+="+AT.smsCenterNm);	
		
		if(AT.smsCenterNm==null)
		{
		    Toast.makeText(NewSMSActivity.this, getResources().getString(R.string.unconnect_wait), Toast.LENGTH_LONG).show();
		    return;
		}						
		if(number.charAt(0)=='+'){
			if((number.charAt(1)=='8')&&(number.charAt(2)=='6'))
			{
				number=number.substring(3);
			}
		}		
		 List<String> texts = SmsManager.getDefault().divideMessage(body);
		 for (String text : texts) {  
			 Intent i = new Intent();
			 i.setAction(ExPhoneService.ACTION_SEND_SMS);
			 i.putExtra("phonenumber", number);
			 i.putExtra("message", text);
			 sendBroadcast(i);
		 }  
		 Toast.makeText(NewSMSActivity.this, getResources().getString(R.string.sms_send_success), Toast.LENGTH_LONG).show();

		 ExSMS q = new ExSMS(this);
		 q.add(number, System.currentTimeMillis(), 2, 1, body);
		 q.close();
	}
	
	public void onClick(View v){
		switch(v.getId()){
		case R.id.send_sms:
			String number = ((EditText)findViewById(R.id.receiver)).getText().toString();
			String msg = ((EditText)findViewById(R.id.sms_edit)).getText().toString();
			switch(mSimCard){
			case 0:
				sendMessage1(number, msg);
				((EditText)findViewById(R.id.sms_edit)).setText("");
				break;
			case 1:
				sendMessage2(number, msg);
				((EditText)findViewById(R.id.sms_edit)).setText("");
				break;
			}
			break;
		}
	}
}
